package com.example.myapplication.view;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myapplication.App;
import com.example.myapplication.R;
import com.example.myapplication.presenter.DetailPresenter;

import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 *
 */
public class DetailActivity extends AppCompatActivity implements DetailView {

    private ViewPager vp;
    private TextView count;
    private DetailPresenter dpr;
    private List<ImageView> imgs;
    private int width;
    private App app;

    public DetailActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setActionBar();
        vp = findViewById(R.id.vp);
        count = findViewById(R.id.point);
        dpr = new DetailPresenter(this);
        app = (App) getApplicationContext();
        setListner();
        setData();
    }

    /**
     * 添加监听
     */
    private void setListner() {
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                count.setText((position+1) + "/" + imgs.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 配置数据
     */
    private void setData() {
        width = app.getwWidth();
        imgs = new ArrayList<>();
        dpr.initViewPagerAdapter(vp);
        count.setText("1/"+imgs.size());
    }

    /**
     * 设置头部栏
     */
    private void setActionBar() {
        ActionBar ab = getSupportActionBar();//添加返回按钮
        if (ab != null) {
            ab.setHomeButtonEnabled(true);
            ab.setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * 添加返回按钮
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initViewPagerAdapter(ViewPager vp, List<Integer> list) {
        for (int i = 0; i < list.size(); i++) {
            Bitmap bp = BitmapFactory.decodeResource(getResources(), list.get(i));
            ImageView iv = new ImageView(this);
            iv.setImageResource(list.get(i));
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width, width * bp.getHeight() / bp.getWidth());
            iv.setLayoutParams(lp);
            vp.setLayoutParams(new ConstraintLayout.LayoutParams(MATCH_PARENT, width * bp.getHeight() / bp.getWidth()));
            iv.setScaleType(ImageView.ScaleType.FIT_START);
            imgs.add(iv);
        }
        vp.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgs.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(imgs.get(position));
                return imgs.get(position);
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(imgs.get(position));
            }
        });
    }
}